package com.ybkj.o2o.sales.service.impl;

import com.ybkj.o2o.sales.common.CouponNumberConfig;
import com.ybkj.o2o.sales.common.RandomString;
import com.ybkj.o2o.sales.entity.Coupon;
import com.ybkj.o2o.sales.entity.CouponRule;
import com.ybkj.o2o.sales.mapper.CouponMapper;
import com.ybkj.o2o.sales.service.CouponRuleService;
import com.ybkj.o2o.sales.service.CouponService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * @author Shine.
 */
@Service
public class DefaultCouponService implements CouponService {

    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private CouponRuleService couponRuleService;

    @Override
    public void updateCouponsLine(Long lineId, String lineName) {
        couponMapper.updateCouponLine(lineId, lineName);
    }

    @Override
    public List<Long> findBindingCoupon() {
        return couponMapper.findBindingCoupon();
    }

    @Override
    public Integer couponTimes(Long couponRuleId, String phone, Long passengerId) {
        Integer times = couponMapper.couponTimes(couponRuleId, phone, passengerId);
        return times;
    }

    @Override
    public Coupon findOneCoupon(String appKey, Long couponRuleId) {
        Coupon coupon = couponMapper.findOneCoupon(appKey, couponRuleId);
        return coupon;
    }

    @Override
    public List<Coupon> findOneLineCoupon(List<Long> linesId) {
        return couponMapper.findOneLineCoupon(linesId);
    }

    @Override
    public int updateCouponPriceAndLineId(Long id, String appKey, BigDecimal couponPrice, Long lineId) {
        return couponMapper.updateCouponPriceAndLineId(id, appKey, couponPrice, lineId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void mass(CouponRule couponRule, Integer amount, String appKey) {

        for (int i = 0; i < amount; i++) {
            Coupon coupon = new Coupon();
            coupon.setCouponRuleId(couponRule.getId());
            coupon.setStatus(Coupon.COUPON_STATUS_NOTBIND);

            Long number = couponMapper.getRunningNumber(couponRule.getId(), appKey);
            if (null == number) {
                number = couponMapper.getTotalCountNumber(couponRule.getId(), appKey);
            }
            if (null == number) {
                number = 0L;
            }

            coupon.setRunCode(number + 1);
            coupon.setCode(couponRule.getCode() + CouponNumberConfig.padZero(number + 1));
            coupon.setCodeKey(RandomString.randomNum(4) + "-" + RandomString.randomNum(4) + "-"
                    + RandomString.randomNum(4));
            coupon.setAppKey(appKey);
            if(null != couponRule.getMoney()) coupon.setCouponPrice(couponRule.getMoney());
            if(null != couponRule.getDiscount()) coupon.setCouponDiscount(couponRule.getDiscount());
            couponMapper.insertRecord(coupon);
        }

    }
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void mass(CouponRule couponRule, Integer amount, String appKey, Long lineId, BigDecimal linePrice,Double couponDiscount, String lineName) {
//
//        for (int i = 0; i < amount; i++) {
//            Coupon coupon = new Coupon();
//            coupon.setCouponRuleId(couponRule.getId());
//            coupon.setStatus(Coupon.COUPON_STATUS_NOTBIND);
//
//            Long number = couponMapper.getRunningNumber(couponRule.getId(), appKey);
//            if (null == number) {
//                number = couponMapper.getTotalCountNumber(couponRule.getId(), appKey);
//            }
//            if (null == number) {
//                number = 0L;
//            }
//
//            coupon.setRunCode(number + 1);
//            coupon.setCode(couponRule.getCode() + CouponNumberConfig.padZero(number + 1));
//            coupon.setCodeKey(RandomString.randomNum(4) + "-" + RandomString.randomNum(4) + "-"
//                    + RandomString.randomNum(4));
//            coupon.setAppKey(appKey);
//            coupon.setLineId(lineId);
//            coupon.setLineName(lineName);
//            coupon.setCouponPrice(linePrice);
//            coupon.setCouponDiscount(couponDiscount);
//            couponMapper.insertRecord(coupon);
//        }
//
//    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void giftCoupon(String passengerIds, String couponRuleIds, String amountStr, String phone,
                           String appKey) {

        String[] amounts = amountStr.split(",");
        String[] couponRules = couponRuleIds.split(",");
        String[] passengers = passengerIds.split(",");
        String[] phones = phone.split(",");
        for (int p = 0; p < passengers.length; p++) {
            for (int j = 0; j < couponRules.length; j++) {

                if (StringUtils.isNotBlank(amounts[j])) {

                    CouponRule couponRule = couponRuleService.findOne(Long.parseLong(couponRules[j]), appKey);

                    for (int i = 0; i < Integer.parseInt(amounts[j]); i++) {

                        Coupon coupon = new Coupon();
                        coupon.setCouponRuleId(couponRule.getId());
                        coupon.setStatus(Coupon.COUPON_STATUS_NOTUSE);

                        Long number = couponMapper.getRunningNumber(couponRule.getId(), appKey);
                        if (null == number) {
                            number = 0L;
                        }

                        coupon.setPassengerId(Long.parseLong(passengers[p]));
                        coupon.setPhone(phones[p]);

                        coupon.setRunCode(number + 1);
                        coupon.setCode(couponRule.getCode() + CouponNumberConfig.padZero(number + 1));
                        coupon.setAppKey(appKey);
                        couponMapper.insertRecord(coupon);
                    }
                }
            }
        }
    }

    @Override
    public List<Coupon> findAll(Long ruleId, String appKey) {

        return couponMapper.selectAllByRule(ruleId, appKey);

    }

    @Override
    public Coupon findOne(Long id, String appKey) {

        return couponMapper.selectByPrimaryKey(id, appKey);
    }

    @Override
    public Coupon findOneByCode(String code, String appKey) {

        return couponMapper.selectByCode(code, appKey);
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(Coupon coupon) {
        couponMapper.updateByPrimaryKey(coupon);

    }

    @Override
    public List<Coupon> selectByRuleWithParams(Long ruleId, String code, String phone,
                                               Integer offset, Integer size, String appKey) {
        return couponMapper.selectByRuleWithParams(ruleId, code, phone, offset, size, appKey);
    }

    @Override
    public long countByRuleWithParams(Long ruleId, String code, String phone, String appKey) {
        return couponMapper.countByRuleWithParams(ruleId, code, phone, appKey);
    }

    @Override
    public Long getRunningNumber(Long couponRuleId, String appKey) {

        return couponMapper.getRunningNumber(couponRuleId, appKey);
    }

    @Transactional(rollbackFor = Exception.class)
    public Long insertRecord(Coupon coupon) {

        return couponMapper.insertRecord(coupon);
    }

    @Override
    public Long countByRuleAndPassenger(Long couponRuleId, Long passengerId) {

        return couponMapper.countByRuleAndPassenger(couponRuleId, passengerId);

    }

    @Override
    public Long countByRuleAndPassengerGroup(Long couponRuleId, Long passengerId) {
        return couponMapper.countByRuleAndPassengerGroup(couponRuleId, passengerId);
    }

    @Override
    public List<Coupon> listValidByPassenger(Long passengerId, String appKey) {

        return couponMapper.listValidByPassenger(passengerId, appKey);
    }


    @Override
    public List<Coupon> selectByPassenger(
            Long passengerId, Integer status, String couponType, Integer offset, Integer size, String appKey) {
        return couponMapper.selectByPassenger(passengerId, status, couponType, offset, size, appKey);
    }

    @Override
    public long countByPassenger(Long passengerId, Integer status, String couponType, String appKey) {
        return couponMapper.countByPassenger(passengerId, status, couponType, appKey);
    }


}
